<?

include '../includes/specs_functions.php';
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;
$THEME = $_POST["THEME"];
#---------------------------------------------------

$stat = "<row status='0' message='NOLOGIN'></row>";
$specId = $_POST['specId'];$mainJob = $_POST['mainJob'];
$layout = $_POST['layout'];
$description = $_POST['description'];
$level = $_POST['level'];
$encodedPNGData = $_POST['encodedPNGData'];

if($CICUID != "" && isset($_POST['level']) && isset($_POST['description']) && isset($_POST['layout']) && isset($_POST['mainJob'])){	
	if(isset($_POST['specId']) && $_POST['specId'] != "" && $_POST['specId'] != -1){		/* INSERT */				$table = SPEC_TBL_JOBS;		$col = array(
						SPEC_TBL_JOBS_ID." as jobId"
					);		$where = array(
						SPEC_TBL_JOBS_TRI ."= \"".$mainJob."\""
					);		$orderBy = SPEC_TBL_JOBS_ID;		$dir = "ASC";				$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $dir, null);		foreach ($result[3] as $row) {			$jobId = $row["jobId"];
					}				$table = SPEC_TBL_SPECS;		$data = array(
						SPEC_TBL_SPECS_LAYOUT ."= \"".$layout."\""
						,SPEC_TBL_SPECS_DESCRIPTION ."= \"".$description."\"" 
						,SPEC_TBL_SPECS_LEVEL ."= \"".$level."\"" 
						,SPEC_TBL_SPECS_JOB_ID ."= \"".$jobId."\""
					);		$where = array(
						SPEC_TBL_SPECS_ID ."= \"".$specId."\""
						,SPEC_TBL_SPECS_USER_ID ."= \"".$CICUID."\""
					);				$result = $DB->Update($table ,$data ,$where);				if ($result[0][1] == 0){
			//No error
			$stat = "<row status='1' message='UPDATE'></row>";			saveImage($encodedPNGData, 0.5, $specId);					}
	}else{				$table = SPEC_TBL_SPECS;		$col = array(
						SPEC_TBL_SPECS_ID." as specId"
					);		$where = array(
						SPEC_TBL_SPECS_USER_ID ."= \"".$CICUID."\""
					);		$orderBy = SPEC_TBL_SPECS_ID;		$dir = "ASC";				$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $dir, null);				if($result[0] < 7){			$table = SPEC_TBL_JOBS;			$col = array(
							SPEC_TBL_JOBS_ID." as jobId"
						);			$where = array(
							SPEC_TBL_JOBS_TRI ."= \"".$mainJob."\""
						);			$orderBy = SPEC_TBL_JOBS_ID;			$dir = "ASC";						$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $dir, null);			foreach ($result[3] as $row) {				$jobId = $row["jobId"];			}					$table = SPEC_TBL_SPECS;			$data = array(
							SPEC_TBL_SPECS_LAYOUT ."= \"".$layout."\""							,SPEC_TBL_SPECS_DESCRIPTION ."= \"".$description."\""							,SPEC_TBL_SPECS_LEVEL ."= \"".$level."\"" 							,SPEC_TBL_SPECS_PUBLIC ."= 'N'"							,SPEC_TBL_SPECS_CDATE ."= \"".date('Y-m-d H:i:s')."\""							,SPEC_TBL_SPECS_UDATE ."= \"".date('Y-m-d H:i:s')."\""							,SPEC_TBL_SPECS_USER_ID ."= \"".$CICUID."\""							,SPEC_TBL_SPECS_JOB_ID ."= \"".$jobId."\""
						);							$result = $DB->Insert($table ,$data);							if ($result[0][1] == 0)
			{				$new_sId = $result[1];
				/* RETRIEVE NEW ID */				saveImage($encodedPNGData, 0.5, $new_sId);				$stat = "<row status='1' message='INSERT' id='".$new_sId."'></row>";							}		}else{			$stat = "<row status='0' message='TOOMANY'></row>";		}
	}
}
echo "<ffxivspecs><status>".$stat."</status></ffxivspecs>";
#------------------------------------------------------------------------# DisconnectleaveDB($DB);
?>